package com.controller;

import com.bean.dto.Dto;
import com.bean.entity.MainGoods;
import com.bean.entity.Specifications;
import com.bean.entity.Supplier;
import com.bean.vo.PageAssistant;
import com.bean.vo.SupplierVo;
import com.service.MainGoodsService;
import com.service.MaterialClassificationService;
import com.service.SpecificationsService;
import com.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("su")
public class SupplierController {
    /* 自动注入 */
    @Autowired
    SupplierService supplierService;
    @Autowired
    SpecificationsService specificationsService;
    @Autowired
    MainGoodsService mainGoodsService;
    @Autowired
    MaterialClassificationService materialClassificationService;
    //查询（条件、分页）

    @RequestMapping("queryAll")
    public Dto queryAll(SupplierVo supplierVo, PageAssistant pa){
        //查询
        System.out.println(">>> 条件、分页查询");
        //创建默认对象
        PageAssistant<SupplierVo> pa1 = new PageAssistant<>();
        SupplierVo supplierVo1 = new SupplierVo();
        //判断分页
        if(pa.getPageSize() != 5){
            pa1.setPageSize(pa.getPageSize());
        }
        if(pa.getCurrPage() != 1){
            pa1.setCurrPage(pa.getCurrPage());
        }
        //查询供应商材料规格详细信息
        if(supplierVo != null){
            return new Dto("0","查询",supplierService.queryAll(pa1,supplierVo),"成功!");
        }else{
            return new Dto("0","查询",supplierService.queryAll(pa1,supplierVo1),"成功!");
        }
    }

    @RequestMapping("query")
    public Dto query(Supplier supplier){
        //查询单个
        System.out.println(">>> 查询详细信息");
        //执行
        try {
            SupplierVo supplierVo = supplierService.query(supplier);
            //查询材料规格详请
            List<Specifications> all = specificationsService.queryAll(new Specifications(supplierVo.getMg().getSpecifications()));
            //写入
            supplierVo.setSpecifications(all);
            return new Dto("0","查询详细信息",supplierVo,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0",e.getMessage(),null,"失败!");
        }
    }

    @RequestMapping("save")
    public Dto save(Supplier supplier){
        //添加
        System.out.println(">>> 添加供应商信息");

        try {
            supplierService.save(supplier);
            return new Dto("0","新增成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","新增失败!",null,"失败!");
        }
    }

    @RequestMapping("remove")
    public Dto remove(Supplier supplier){
        //删除
        System.out.println(">>> 删除供应商信息");

        try {
            supplierService.remove(supplier);
            return new Dto("0","删除成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","删除失败!",null,"失败!");
        }
    }

    @RequestMapping("modify")
    public Dto modify(SupplierVo supplierVo, MainGoods mainGoods){
        //删除
        System.out.println(">>> 修改供应商信息");
        try {
            mainGoods.setTheMainGood(supplierVo.getTheMainGood());
            mainGoodsService.modify(mainGoods);
            supplierService.modify(supplierVo);
            return new Dto("0","修改成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","修改失败!",null,"失败!");
        }
    }
}
